<?php
/*
**2015/5/15 Coded by ZYJ
**随堂练习模块
	参数：
**itemnum    学生的答题量
**itemid     题目编号
**openid     微信用户标识
**exer_item  id:为exercise数据库的id属性，但是从页面返回到recordOption中是使用1-10的编号而不是exercise中的id
	接口：
	Mysql：
*/	
namespace Home\Controller;
use Common\Controller\MyInitController;
class InclassController extends MyInitController {		
    public function index($test_num = 1){
							
		
		/*=========定义变量=======*/
		$openid 	= $this->openid;
		$Exercise 	= D("exercise");
		$Testans   	= D('testans');  
		$Time		=	D("time");		
		
		
		$time	=	$Time->where("isdelete = 0")->getField("test_num,isopen");
		/*=========获取习题一览=======*/
		$exer_info  = $Exercise->distinct(true)->field('test_num')->select();
		
		/*=========计算正确率=======*/
		for($k=0;$k<count($exer_info);$k++){				
			$exer_info_add 	= $this->isExercise($openid,$exer_info[$k]['test_num']);				
			$total 		  	= count($exer_info_add);
			$right 			= 0;
			foreach($exer_info_add as $v){
				$v["right_lab"]? $right++:0;
			}
			$exer_info[$k]["rate"] = $right/$total*100? $right/$total*100:0;
			$exer_info[$k]["statue"]	=	(!$time[$exer_info[$k]['test_num']] == 0)? ' (开启中)':'(未开启)';
			//array_splice($arr1, $key, 1);			
		}
		
		if($exer_item_add = $this->isExercise($openid,$test_num)){
			$exer_item  = $Exercise->distinct(true)->field('id,test_num,question,optA,optB,optC,optD,que_analysis,rightans')->where("test_num = $test_num")->order("id")->select();			
			$i			= 0;

			foreach($exer_item_add as $a){	
				$exer_item[$i] = array_merge($exer_item[$i], $a); 
				$i++;					
			}
		}else{
			$exer_item  = $Exercise->distinct(true)->field('id,test_num,question,optA,optB,optC,optD')->where("test_num = $test_num")->select();
			$this->assign("isExercise",0);
		}		
		
		/*=========获取习题题目=======*/				
		$this->assign("exer_info",$exer_info);
		$this->assign("exer_item",$exer_item);
				
		$this->display("Inclass/index");		
    }
	public function isExercise($openid=0,$test_num=1){
		$_Exercise 	= D("exercise");
		$_Testans   = D('testans');
		
		$counts 	= $_Exercise->field('id,test_num')->where("test_num = $test_num")->count();		
		$result 	= $_Testans->where("openid='{$openid}' AND test_num = {$test_num}")->field('id,testid,test_num,answer,right_lab,time')->limit($counts)->order("id")->select();
		
		return $result;
	}
	public function getSubmitNum(){
		$openid 	= $this->openid;
		$Testans   	= D('testans');
		$Class 		= D("class");
		$className	= $Class->getTableName();
		$test_num 	= I("post.test_num_id");

		//$num =$Testans->where("openid='{$openid}' AND test_num = {$test_num} AND class = {$class} ")->field('time')->find();	
		$num 		= $Testans->query("SELECT count(distinct openid) FROM `__TESTANS__` WHERE class = (select class from `$className` where `openid` = '$openid' limit 1) AND test_num =$test_num"); 				
		$num 		= array_values($num[0]);
		if(IS_AJAX){
			$this->ajaxReturn($num[0]);
		}else return $num;
	}
	public function recordOption(){ //用于记录答题情况		
		$openid 	= $this->openid;
		$Class 		= D("class");				
		$Testans   	= D('testans'); 
		$Exercise 	= D("exercise");
		$Time		=	D("time");		
		$test_num 	= I('post.test_num_id');
		
		$time		=	$Time->where("isdelete = 0")->getField("test_num,isopen");
		$isopen		=	$time[$test_num];
		
		if(!$isopen || !isset($isopen)) $this->error("本测试未在开启状态！");
		
		$class 		= $Class->where("openid='{$openid}'")->getField("class");
		$rightans 	= $Exercise->where("test_num='{$test_num}'")->field("id,rightans")->order("id")->select();	
		
		if($Testans->create()){
			$i=1;
			while(!is_null($_POST[$i])){
				$tmp['testid'] 		= 	$i;
				$tmp['class'] 		= 	$class;
				$tmp['test_num'] 	= 	$test_num;
				$tmp['openid'] 		=	I("post.openid");							
				$tmp['time'] 		= 	date("Y-m-d H:i:s");
				$tmp['answer'] 		= 	end(explode("opt",I("post.$i")));	
				$tmp['right_lab'] 	=	($rightans[$i-1]['rightans'] == $tmp['answer'])? 1:0;
								
				$result = $Testans->add($tmp);				
				if($result) {
					$i++;
				}else{
					$this->error('错误！');
				}			
			}			
			$this->success('完成题目！',U("Inclass/index?test_num=$test_num"));
		}else{
			$this->error($Testans->getError());			
		}
	}
	public function getInclassNum($openid=0){		
		$_Testans   = D('testans');  		
		$inclassnum = $_Testans->distinct(true)->field('test_num')->where("openid='{$openid}'")->select();
		return count($inclassnum);
	}
	public function getRightAns(){
		/*=========判断是否通过ajax方式传输数据=======*/
		if(IS_AJAX){
			/*=========定义变量=======*/
            $itemid   	= I('post.itemid');
            $openid   	= I('post.openid');
            $answer   	= I('post.answer');
            $starttime	= I('post.starttime');
			$Question 	= M('questionbank');
			
			$usedtime 	= abs(strtotime(date("Y-m-d H:i:s"))-strtotime($starttime));
			/*=======获取正确答案=====*/	
			$ajaxreturn['rightans']     = $Question->where("id='{$itemid}'")->getField("rightans");
			$ajaxreturn['que_analysis'] = $Question->where("id='{$itemid}'")->getField("que_analysis");			
			
			/*=======记录答题情况=====*/
			$this->recordOption($answer,$openid,$itemid,$ajaxreturn['rightans'],$usedtime);
		
			/*====返回答案解析到前台==*/
			$this->ajaxReturn($ajaxreturn);     			     
        }
		else $this->ajaxReturn('非法的请求方式'); 		
	}
	public function getItemNum($openid){
		$_Answer   = D('answer');
		$itemnum  =   $_Answer->where("openid='{$openid}'")->count();
		return $itemnum;
	}
	
	public function isCollect($data=''){
		if(empty($data)) {
            $data   =   I('post.');
        }elseif(is_object($data)){
            $data   =   get_object_vars($data);
        }
		if(empty($data) || !is_array($data)) {
            $this->error = L('_DATA_TYPE_INVALID_');
            return false;
        }
		/*====定义变量实例化==*/
		$_Collect = D("collect");
					
		$collectData["openid"]    = $data['openid'];
		$collectData["itemid"]    = $data['itemid'];
		/*====判断是否存在该题目==*/
		$iscollect = $_Collect->where($collectData)->find();
		if(!is_null($iscollect)) {			
			return $iscollect["time"];
		}else return 0;
	}
	public function changeMysql(){
		
	}
	
	
	
	
}




?>